
package edu.chl.vrangogym.web.util;

import edu.chl.vrangogym.persistence.entities.enums.Permissions;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

/**
 * Used in jsf to convert Permissions to a string and vice versa.
 * 
 * @author Anders
 */
@FacesConverter(value = "PermissionConverter")
public class PermissionConverter implements Converter {

    public PermissionConverter(){
    }
    
    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        for(Permissions p : Permissions.values()) {
            if(value.equals(p.toString())) {
                return p;
            }
        }
        return null;
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        return value.toString();
    }
}
